﻿#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Linq;
using AK.Wwise;
using AudioStudio.Components;
using DavidJalbert.TinyCarControllerAdvance;
using ImageEffects;
using UnityEditor;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gameplay.Liberty.Media;
using Yoozoo.Gameplay.Liberty.PlayerController;
using Yoozoo.Gameplay.Liberty.VehicleController;
using Object = UnityEngine.Object;

namespace com.yoozoo.gta.Tools.Car
{
    public class CarTools
    {
        [MenuItem("GameObject/Car/构建车辆",false,10)]
        public static void ConstructCar()
        {
            var gos = Selection.objects;
            foreach (var go in gos)
            {
                try
                {
                    ConstructCar(go as GameObject);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
        }

        public static void ConstructCar(GameObject go)
        {
            PrefabUtility.UnpackPrefabInstance(go, PrefabUnpackMode.OutermostRoot, InteractionMode.UserAction);
            LODGroup lodGroup = go.GetComponentInChildren<LODGroup>();
            if (lodGroup)
            {
                lodGroup.enabled = false;
            }

            go.transform.position = Vector3.zero;
            go.transform.eulerAngles = Vector3.zero;
            
            GameObject root = new GameObject(go.name + "_Root");
            go.transform.SetParent(root.transform);

            TCCAPlayer tccaPlayer = go.AddComponent<TCCAPlayer>();
            
            Transform car = go.transform.GetChild(0);
            
            CarEntity carEntity = root.AddComponent<CarEntity>();
            carEntity.carBase = car.GetChild(0);


            VehicleSetting vehicleSetting = root.GetOrAddComponent<VehicleSetting>();
            
            Transform frontPoint = new GameObject("frontPoint").transform;
            frontPoint.SetParent(car);
            frontPoint.localPosition = new Vector3(0, 0.5f, 2.64f);
            frontPoint.localRotation = Quaternion.identity;
            
            Transform backPoint = new GameObject("backPoint").transform;
            backPoint.SetParent(car);
            backPoint.localPosition = new Vector3(0, 0.69f, -2.7f);
            backPoint.localRotation = Quaternion.identity;

            Transform leftDoorPoint = new GameObject("leftDoorPoint").transform;
            leftDoorPoint.SetParent(car);
            leftDoorPoint.localPosition = new Vector3(-1.543f, 0.04f, -0.32f);
            leftDoorPoint.localEulerAngles = new Vector3(0, 90, 0);

            Transform rightDoorPoint = new GameObject("rightDoorPoint").transform;
            rightDoorPoint.SetParent(car);
            rightDoorPoint.localPosition = new Vector3(1.778f, 0.04f, -0.42f);
            leftDoorPoint.localEulerAngles = new Vector3(0, -90, 0);

            Transform leftSeatPoint = new GameObject("leftSeatPoint").transform;
            leftSeatPoint.SetParent(car);
            leftSeatPoint.localPosition = new Vector3(-0.4145f, 0.039f, 0.184f);
            leftDoorPoint.localEulerAngles = new Vector3(0, -12.596f, 0);

            vehicleSetting.frontPoint = frontPoint;
            vehicleSetting.backPoint = backPoint;
            vehicleSetting.leftDoorOpenPoint = leftDoorPoint;
            vehicleSetting.rightDoorOpenPoint = rightDoorPoint;
            vehicleSetting.leftSeatPoint = leftSeatPoint;

            VehicleAI vehicleAI = root.AddComponent<VehicleAI>();
            // AI暂时不处理
            vehicleAI.enabled = false;

            VehicleAudioComponent audioComponent = root.AddComponent<VehicleAudioComponent>();

            GameObject audioEmitter = new GameObject("AudioEmitter");
            audioEmitter.transform.SetParent(car);
            var audioEmitter3D = audioEmitter.AddComponent<AudioEmitter3D>();
            audioComponent.audioEmitter3D = audioEmitter3D;

            LightElement lightElement = root.AddComponent<LightElement>();
            lightElement.FindLightRenderer(false);

            lightElement.controllerMark = "pveScene";

            VehicleInteriorComponent interiorComponent = root.AddComponent<VehicleInteriorComponent>();
            interiorComponent.FindInteriors(false);

            VehicleLodComponent lodComponent = root.AddComponent<VehicleLodComponent>();

            
            // car body

            TCCABody tccaBody = car.gameObject.AddComponent<TCCABody>();

            BoxCollider carCollider = car.gameObject.AddComponent<BoxCollider>();
            carCollider.material = 
                AssetDatabase.LoadAssetAtPath<PhysicMaterial>("Assets/Scripts/External/DavidJalbert/TinyCarControllerAdvance/Physics/Body Material.physicMaterial");

            carCollider.center = new Vector3(0, 0.83f, 0);
            carCollider.size = new Vector3(1.93f, 1.26f, 4.95f);

            LoopAdjustToneSound adjustToneSound = car.gameObject.AddComponent<LoopAdjustToneSound>();

            VehicleCollisionSound collisionSound = car.gameObject.AddComponent<VehicleCollisionSound>();

            
            // 车轮
            List<GameObject> wheels = new List<GameObject>();
            for (int i = 0; i < car.childCount; i++)
            {
                var cgo = car.GetChild(i).gameObject;
                if (cgo.name.Contains("W_LB") || 
                    cgo.name.Contains("W_LF") || 
                    cgo.name.Contains("W_RB") || 
                    cgo.name.Contains("W_RF")
                    )
                {
                    wheels.Add(cgo);
                }
            }

            foreach (var wheel in wheels)
            {
                var wheelParent = new GameObject(wheel.name);
                wheelParent.transform.SetParent(wheel.transform.parent);
                wheelParent.transform.localPosition = wheel.transform.localPosition;
                wheelParent.transform.localEulerAngles = wheel.transform.localEulerAngles;
                wheel.transform.SetParent(wheelParent.transform);
                wheel.transform.localPosition = Vector3.zero;
                wheel.transform.localEulerAngles = Vector3.zero;
                
                var tccaWheel = wheelParent.AddComponent<TCCAWheel>();
                tccaWheel.wheelMaterial = 
                    AssetDatabase.LoadAssetAtPath<PhysicMaterial>("Assets/Scripts/External/DavidJalbert/TinyCarControllerAdvance/Physics/Wheels Material.physicMaterial");
                tccaWheel.wheelRadius = 0.36f;
                tccaWheel.wheelMass = 1;

                if (wheel.name.Contains("W_LB") || wheel.name.Contains("W_RB") )
                {
                    tccaWheel.steeringEnabled = false;
                    tccaWheel.motorEnabled = false;
                    tccaWheel.handbrakeEnabled = true;
                }
                else
                {
                    tccaWheel.steeringEnabled = true;
                    tccaWheel.motorEnabled = true;
                    tccaWheel.handbrakeEnabled = false;
                }
            }

            carEntity.vehicleAI = carEntity.GetComponent<VehicleAI>();
            carEntity.vehicleSetting = carEntity.GetComponent<VehicleSetting>();
            carEntity.carController = carEntity.GetComponentInChildren<TCCAPlayer>();
            carEntity.InteriorComponent = carEntity.GetComponent<VehicleInteriorComponent>();
            carEntity.vehicleLodComponent = carEntity.GetComponent<VehicleLodComponent>();

            audioComponent.loopAdjustToneSound = carEntity.GetComponentInChildren<LoopAdjustToneSound>();
            audioComponent.vehicleColSound = carEntity.GetComponentInChildren<VehicleCollisionSound>();

            //vehicleAI.carBody = tccaBody;
            vehicleAI.carController = tccaPlayer;

            vehicleSetting.inputCameraConfig = new InputCameraConfig();
            vehicleSetting.inputCameraConfig.initializedData = new CameraInitializedData();
            vehicleSetting.inputCameraConfig.followTarget = new CameraFollowTarget();
            vehicleSetting.inputCameraConfig.initializedData.cameraAngle = new Vector3(10, 0, 0);
            vehicleSetting.inputCameraConfig.followTarget.targetRoot = carEntity.transform;


            for (int i = 0; i < tccaPlayer.transform.childCount; i++)
            {
                if (tccaPlayer.transform.GetChild(i).gameObject.name.Contains("_LOD1"))
                {
                    lodComponent.lod1MeshRenderers =
                        tccaPlayer.transform.GetChild(i).gameObject.GetComponents<MeshRenderer>().ToList();
                    tccaPlayer.transform.GetChild(i).SetParent(carEntity.carBase);
                    break;
                }
            }

            lodComponent.lod0MeshRenderers = car.GetComponentsInChildren<MeshRenderer>().ToList();
        }
        
    }
}
#endif
